#ifndef RTCSLEEP_H
#define RTCSLEEP_H

//#include "stm32f10x_conf.h"
#include "stm32f10x.h"
#include "string.h"
#include "stm32f10x_rtc.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_dbgmcu.h"
#include "misc.h"
#include "rcc.h"
#include "nvic.h"

/********************************************************
 * referfrom board_info.h
 *
********************************************************/
#define DISABLE_INTERRUPTS()    __disable_irq();
#define ENABLE_INTERRUPTS()     __enable_irq();
/********************************************************/

/*from radiotimer.h*/
#define PORT_TIMER_WIDTH                    uint16_t
#define PORT_RADIOTIMER_WIDTH               uint16_t
typedef void (*radiotimer_compare_cbt)(void);
typedef void (*radiotimer_capture_cbt)(PORT_TIMER_WIDTH timestamp);


/*from board.h*/
//=========================== define ==========================================
typedef enum {
   DO_NOT_KICK_SCHEDULER,
   KICK_SCHEDULER,
} kick_scheduler_t;
//=========================== typedef =========================================

//=========================== variables =======================================

//=========================== prototypes ======================================

// admin
void     radiotimer_init(void);
void     radiotimer_setOverflowCb(radiotimer_compare_cbt cb);
void     radiotimer_setCompareCb(radiotimer_compare_cbt cb);
void     radiotimer_setStartFrameCb(radiotimer_capture_cbt cb);
void     radiotimer_setEndFrameCb(radiotimer_capture_cbt cb);
void     radiotimer_start(PORT_RADIOTIMER_WIDTH period);
// direct access
PORT_RADIOTIMER_WIDTH radiotimer_getValue(void);
void     radiotimer_setPeriod(PORT_RADIOTIMER_WIDTH period);
PORT_RADIOTIMER_WIDTH radiotimer_getPeriod(void);
// compare
void     radiotimer_schedule(PORT_RADIOTIMER_WIDTH offset);
void     radiotimer_cancel(void);
// capture
PORT_RADIOTIMER_WIDTH radiotimer_getCapturedTime(void);

// interrupt handlers
kick_scheduler_t   radiotimer_isr(void);


/**
 * @brief RTC休眠初始化
 */
void rtcSleepInit(void);

/**
 * @brief RTC休眠
 * @param ulMs 休眠时间，单位ms
 * @note RTC寄存器实际为24位，休眠时间为1~0xFFFFFF
 *       延时精度未作精确处理
 *       1ms延时的误差会比较大，10ms以上的延时，误差应在5%以内
 */
void rtcSleep(uint32_t ulMs);

#endif // RTCSLEEP_H
